import { JSEncrypt } from 'jsencrypt'

const privateKey = ''

/**
 * @description RSA加密(支持长字符加密)
 * @param data
 * @returns {Promise<{param: PromiseLike<ArrayBuffer>}|*>}
 */
export async function encryptedData(data) {
  const publicKey =
    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhBBQC2oBO0GXFCYOTetDxigSftHVfa2va7Ui/7QNuHzvpu8h9Y9L3cfcdJsiUKfls1+W9DXN5jpdket+L4pN+U5gK4BGCwYC+ZLdZ6RFs807EM32CYiRdz9iZCMdyzsnST0eCxYg5o2j949oO0aqake1n5PSqUofcDV/QlK+NPj1UYMcah564s3tNynr5bq4RDE9KmWSIJQp2O1Xq0T9VGsSgb4khw2F9/Bw3lYvM/xnyaRqWhApMbeofqZ16y05rUEmMuldwSTDSDs46qvLtUAo/KDKISLCA+/DMoqzD48BJ13rX3yJTpi6P2OOc33BnBS1S/EikSWjWPaC0Mag+QIDAQAB'
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(
    `-----BEGIN PUBLIC KEY-----${publicKey}-----END PUBLIC KEY-----`
  )
  data = encrypt.encrypt(JSON.stringify(data))
  return data
}

/**
 * @description RSA解密(支持长字符解密)
 * @param data
 * @returns {PromiseLike<ArrayBuffer>}
 */
export function decryptedData(data) {
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(
    `-----BEGIN RSA PRIVATE KEY-----${privateKey}-----END RSA PRIVATE KEY-----`
  )
  return decrypt.decrypt(JSON.parse(data))
}
